package com.yimo.util;

import com.yimo.common.exception.BaseErrors;
import com.yimo.common.exception.BusinessException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class DownloadUtil {

    public static void downloadFile(String downPath, String exportFileName, HttpServletResponse response) {

//        response.setContentType("multipart/form-data");
        response.setContentType("application/vnd.openxmlformats");

        response.setHeader("Content-Disposition", "attachment;fileName=" + exportFileName);
        ServletOutputStream out;
        File file = new File(downPath);
        try {
            FileInputStream inputStream = new FileInputStream(file);

            out = response.getOutputStream();

            int b;
            byte[] buffer = new byte[512];
            while ((b = inputStream.read(buffer)) != -1) {

                out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            throw new BusinessException(BaseErrors.SYSTEM_ERROR);
        }
    }

    public static void main(String[] args) {
        Map <String, Object> map = new HashMap <>();
        add(map);
        System.out.println(map);
    }

    public static void add(Map <String, Object> map) {

        map.put("11", "11");
    }
}
